package com.cn.yunhe.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSONObject;
import com.cn.yunhe.dao.impl.UserDaoI;
import com.cn.yunhe.moder.UserModer;
import com.cn.yunhe.service.impl.UserServiceI;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserController extends ActionSupport implements ModelDriven<Object>{

	private static final long serialVersionUID = 1L;
	//因为extends ActionSupport(可以获取值通过getter方法)，所以这里可以UserModer等等为属性值。其他都不可以，必须通过new来获取
	private UserModer user;
	private List<UserModer> list;
	private String message;
	public List<UserModer> getList() {
		return list;
	}
	public void setList(List<UserModer> list) {
		this.list = list;
	}
	public UserModer getUser() {
		return user;
	}
	public void setUser(UserModer user) {
		this.user = user;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * 猪唠....记得这个一定不能返回null
	 */
	public Object getModel() {
		// TODO Auto-generated method stub
		if (user==null) {
			user=new UserModer();
		}
		return user;
	}
	
	/**
	 * 用户登陆
	 */
//	public String queryUser()throws IOException{
//		
//		 HttpServletResponse response=ServletActionContext.getResponse();  
//	        response.setContentType("text/html");  
//	        PrintWriter out;  
//	        out = response.getWriter();  
//		if("王小乐".equals(user.getUserName()) && "211221".equals(user.getPwdword())){
//			this.setMessage("登陆成功！");
//			return "queryUser";
//   	}
//    	
//        this.setMessage("登陆失败！");
//        return "error";
//	}
	/**
	 * 用户显示
	 * @return
	 */
	public String finduser() {
		
		list= new UserDaoI().selectAll();
		
		HttpServletResponse response=ServletActionContext.getResponse(); 
		HttpServletRequest request= ServletActionContext.getRequest();
		PrintWriter out=null;
		response.setCharacterEncoding("utf-8");
		 try {
			 out = response.getWriter();  
			 request.setCharacterEncoding("utf-8");
			 request.setAttribute("list", list);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		 return SUCCESS;
	}
	/**
	 * 修改信息，根据id修改用户信息
	 * @return
	 */
	public String updateuser(){

		boolean  flag=new UserServiceI().updateUser(user);
		if(flag){
		return "success";
		}
		return "error";	
	}
	/**
	 * 获取用户的id
	 * @return
	 */
	public String updateuserById(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String userId=request.getParameter("userId");
		user = new UserServiceI().finduserById(Integer.parseInt(userId));
		request.setAttribute("user", user);
		return "success";
	}
	
	/**
	 * 添加用户
	 * @return
	 */
	public String insertuser(){
		boolean  flag=new UserServiceI().insertUser(user);
		if(flag){
		return "success";
		}
		return "error";	
		
	} 
	/**
	 * 删除用户
	 * @return
	 */
	public String deleteuser(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		PrintWriter out=null;
		String userId=request.getParameter("userId");
		try {
			out = response.getWriter(); 
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");	
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		boolean  flag=new UserServiceI().deleteUser(Integer.parseInt(userId));
		JSONObject json=new JSONObject(); 
		if(flag){
			 json.put("userId", userId);  
		     json.put("success", true);  
		   
		    out.println(json.toString()); 
	
		}
		
		
		out.flush();
		out.close();
		 return SUCCESS;
		
	}
	}
	

